package com.web.music.portal.mvc;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.web.music.interfaces.SecurityController;
import com.web.music.interfaces.UserService;
import com.web.music.model.RegisterForm;
import com.web.music.model.User;

public class RegisterFormController extends SimpleFormController {
	private SecurityController securityController;
	private UserService userService;

	public RegisterFormController() {
		setCommandClass(RegisterForm.class);
		setSessionForm(true);
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request) {
		RegisterForm form = new RegisterForm();
		return form;
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object object, BindException errors)
			throws Exception {
		RegisterForm form = (RegisterForm) object;
		User user = new User();
		user.setUserName(form.getLogin());
		user.setPassword(form.getPassword());
		user.setRegistered(new Date());
		user.setIsAdmin("N");
		userService.save(user);
		securityController.doLogin(request, user.getUserName(), user.getPassword());
		return super.onSubmit(request, response, object, errors);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SecurityController getSecurityController() {
		return securityController;
	}

	public void setSecurityController(
			SecurityController securityController) {
		this.securityController = securityController;
	}



}
